package zjut.cocouml.command;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateConnectionRequest;

import zjut.cocouml.model.BClass;
import zjut.cocouml.model.BInterface;
import zjut.cocouml.model.Compartment;
import zjut.cocouml.model.relationship.BRelationship;


public class CreateImplementsConnectionCommand extends Command {

	private Compartment source,target;
	private BRelationship connection;
	
	public boolean canExecute() {
		if (source == null || target == null) 
			return false;
		if (source == target)
			return false;
		return canCreate(source,target);
	}
	
	private boolean canCreate(Compartment source,Compartment target) {
		
		if ( source instanceof BClass && target instanceof BInterface) {
			if (source.getTargetConnections().contains(connection) ) {
				return false;
			}
			return true;
		}
		return false;
	} 
	
	public void execute() {
		//System.out.println("CreateConnectionCommand:: execute...");
		connection.attachSource();
		connection.attachTarget();
		connection.addRelationship(source, target);
	}
	
	public void setConnection(Object model) {
		connection = (BRelationship) model;
	}
	public void setSource(Object model) {
		source = (Compartment) model;
		connection.setSourceModel(source);
	}
	
	public void setTarget(Object model) {
		target = (Compartment) model;
		connection.setTargetModel(target);
	}
	
	public void undo() {
		connection.detachSource();
		connection.detachTarget();
		connection.removeRelationship(source, target);
	}
}
